#!/usr/bin/python

class MyBin:
    value = []
    value_int = 0
    # def __init__(self, val_str): # 
    
    # def __init__(self, val):
    #     s = bin(val)
    #     self.value = []              # need to clear ??
    #     for i in s[len(s)-1:1:-1]:
    #         self.value.append(int(i))

    def __init__(self, val_str):
        if type(val_str) == type([]):
            self.value = val_str
            return None
        fields = val_str.split("'")
        width = int(fields[0], 10)
        if fields[1][0] == 'h':
            base = 16
        elif fields[1][0] == 'd':
            base = 10
        elif fields[1][0] == 'b':
            base = 2
        else:
            assert(False)

        self.value_int = int(fields[1][1:].replace("_", ""), base)
        val_tmp = self.value_int
        self.value = []              # need to clear ??
        for i in range(width):
            self.value.append(val_tmp & 1)
            val_tmp >>= 1
        assert(val_tmp==0)      # otherwise, format not correct

    # def __init__(self, val_array):
    #     self.value = val_array

    def __repr__(self):
        res = []
        for i,v in enumerate(self.value):
            if i and (i%4==0):
                res.append('_')
            res.append(str(v))
        res.reverse()
        return ''.join(res)

    @staticmethod
    def verilog_bin2int(str_val):
        str_val = str_val.replace('_', '')
        return int(str_val, 2)

    # def bin2int(self):          # 
        
    def get_value(self):
        return self.value

    def __len__(self):
        return len(self.value)

    def __add__(self, mb):
        # print mb.get_value()
        assert(len(mb) == self.__len__()) # length must be same
        mb_value = mb.get_value()
        sum = []
        carry = 0
        for i in range(len(self.value)):
            s = self.value[i] + mb_value[i] + carry
            sum.append( s%2 )
            if s>1:
                carry = 1
            else:
                carry = 0
        if carry:
            print "overflow !"
        return MyBin(sum)
            
            
            
            
        # return self           # also valid ??

# a = MyBin("32'h1234_5678")
# val_str = "%s" % a
# print val_str

# val = MyBin.verilog_bin2int(val_str)
# print "%x" % val


# a = MyBin("16'h1234").get_value()
# b = MyBin("16'h5678").get_value()

# a = MyBin("8'hab").get_value()
# b = MyBin("8'h56").get_value()

ma = MyBin("8'hab")
mb = MyBin("8'h56")
k = ma + mb

print "%s + %s = %s" % (ma, mb, k)


# a.append(a[-1])
# b.append(b[-1])                 # multiplier
# b.append(b[-1])

# r = a +                         # 
# print r
# print a
# print b

b_ext = [0] + b                 # extned for booth 2
booth_array = []
for i in range(0,8+2,2):        # 0..8
    booth_array.append(b_ext[i] + b_ext[i+1] - 2*b_ext[i+2])

pp = []
# for i in booth_array:
#     if i == 0:
#         pp.append([0]*len(a))
#     elif i == 1:
#         pp.append(a)
#     elif i == 2:
#         pp.append([0] + a[:-1])
#     elif i == -1:
#         pp.append([for i in a]

